package cd.cdyb.sms.service.impl;

import cd.cdyb.sms.constant.RedissonConstant;
import cd.cdyb.sms.dao.SystemConfigDao;
import cd.cdyb.sms.domain.SystemConfig;
import cd.cdyb.sms.dto.SystemConfigDto;
import cd.cdyb.sms.dto.expand.ExSystemConfigDto;
import cd.cdyb.sms.exception.BusinessException;
import cd.cdyb.sms.service.SystemConfigService;
import cd.cdyb.sms.utils.BeanUtilPlus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lin
 * @since 2024-05-21
 */
@Service
public class SystemConfigServiceImpl implements SystemConfigService {

    @Resource
    private SystemConfigDao systemConfigDao;

    @Resource
    private RedissonClient redisson;


    @Override
    public void updateSystemConfig(SystemConfigDto systemConfigDto) {
        LambdaUpdateWrapper<SystemConfig> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(SystemConfig::getEmail, systemConfigDto.getEmail())
                .set(SystemConfig::getPhone, systemConfigDto.getPhone())
                .set(SystemConfig::getIndustryPrompt, systemConfigDto.getIndustryPrompt())
                .set(SystemConfig::getMarketingPrompt, systemConfigDto.getMarketingPrompt());
        systemConfigDao.update(null, wrapper);
    }

    @Override
    public SystemConfigDto getSystemConfig() {
        SystemConfig systemConfig = systemConfigDao.selectOne(new LambdaQueryWrapper<>());
        return BeanUtilPlus.copyAs(systemConfig, SystemConfigDto.class);
    }

    @Override
    public void recharge(ExSystemConfigDto exSystemConfigDto) throws BusinessException {
        RLock lock = redisson.getLock(RedissonConstant.SYSTEM_CONFIG_LOCK);
        lock.lock();
        try {
            SystemConfig systemConfig = systemConfigDao.selectOne(new LambdaQueryWrapper<>());
            if (Objects.isNull(systemConfig)) {
                throw new BusinessException("未查询到配置信息");
            }
            LambdaUpdateWrapper<SystemConfig> wrapper = new LambdaUpdateWrapper<>();
            if (5 == exSystemConfigDto.getRechargeType()) {
                systemConfig.setMarketingCount(systemConfig.getMarketingCount() + exSystemConfigDto.getRechargeQuantity());
                wrapper.set(SystemConfig::getMarketingCount, systemConfig.getMarketingCount());
            } else if (6 == exSystemConfigDto.getRechargeType()) {
                systemConfig.setIndustryCount(systemConfig.getIndustryCount() + exSystemConfigDto.getRechargeQuantity());
                wrapper.set(SystemConfig::getIndustryCount, systemConfig.getIndustryCount());
            } else {
                throw new BusinessException("充值类型异常");
            }
            systemConfigDao.update(null, wrapper);
        } catch (Exception e) {
            throw new BusinessException(e.getMessage());
        } finally {
            lock.unlock();
        }
    }
}
